package com.sskj.gocoin.v.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;

import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.p.activity.VerifyIdentPresenter;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.FileUtil;
import com.sskj.gocoin.utils.PictureUtil;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.base.BaseActivity;

import java.io.File;
import java.util.HashMap;

import butterknife.BindView;

public class VerifyIdentActivity extends BaseActivity<VerifyIdentPresenter> {


    private static final int REQUEST_A_CAMERA = 1000;
    private static final int REQUEST_B_CAMERA = 1001;
    private static final int REQUEST_C_CAMERA = 1002;
    private static final String NAME_A = "photo_a.jpg";
    private static final String NAME_B = "photo_b.jpg";
    private static final String NAME_C = "photo_c.jpg";
    @BindView(R2.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R2.id.verify_photo_a)
    ImageView mVerifyPhotoA;
    @BindView(R2.id.verify_photo_b)
    ImageView mVerifyPhotoB;
    @BindView(R2.id.verify_submit)
    Button mVerifySubmit;
    private String mCurrFileName;

    private HashMap<String, Object> mParams = new HashMap<String, Object>();

    public static void start(Context context) {
        Intent intent = new Intent(context, VerifyIdentActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_verify_ident;
    }

    @Override
    public VerifyIdentPresenter getPresenter() {
        return new VerifyIdentPresenter();
    }

    @Override
    protected void onViewBinding() {
        mToolBar.setTitle(App.INSTANCE.getString(R.string.strVerifyIdentActivity203));
        mToolBar.setLeftButtonOnClickLinster(v -> finish());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            String filePath = null;
            switch (requestCode) {
                case REQUEST_A_CAMERA://人证合一
                    filePath = new File(getFilesDir(), NAME_A).getAbsolutePath();
                    break;
                case REQUEST_B_CAMERA://身份证背面
                    filePath = new File(getFilesDir(), NAME_B).getAbsolutePath();
                    break;
                case REQUEST_C_CAMERA: //身份证背面
                    filePath = new File(getFilesDir(), NAME_C).getAbsolutePath();
                    break;
            }
            if (TextUtils.isEmpty(filePath)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyIdentActivity205));
                return;
            }

            Bitmap smallBitmap = PictureUtil.getSmallBitmap(filePath);
            PictureUtil.saveBitmap(smallBitmap, 70, filePath, Bitmap.CompressFormat.JPEG);
            File file = new File(filePath);
            if (file != null && smallBitmap != null) {
                setFilePath(smallBitmap, filePath, file);
            }
        }

    }


    @Override
    protected void initView() {
        ClickUtil.click(mVerifyPhotoA, () -> {
            mCurrFileName = NAME_A;
            Intent intent = new Intent(this, CameraActivity.class);
            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtil.getSaveFile(getApplication(), NAME_A).getAbsolutePath());
            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intent, REQUEST_A_CAMERA);
        });
        ClickUtil.click(mVerifyPhotoB, () -> {
            mCurrFileName = NAME_B;
            Intent intent1 = new Intent(this, CameraActivity.class);
            intent1.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtil.getSaveFile(getApplication(), NAME_B).getAbsolutePath());
            intent1.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
            startActivityForResult(intent1, REQUEST_B_CAMERA);
        });

        ClickUtil.click(mVerifySubmit, () -> {
            if (mParams.size() < 2) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyIdentActivity220));
                return;
            }
            mPresenter.submitVerify(mParams);
        });
    }

    private void setFilePath(Bitmap smallBitmap, String filePath, File file) {
        if (NAME_A.equals(mCurrFileName)) {
            mParams.put("photo_a", file);
            mParams.put("photoAName", mCurrFileName);
            mVerifyPhotoA.setImageBitmap(smallBitmap);
        } else if (NAME_B.equals(mCurrFileName)) {
            mParams.put("photo_b", file);
            mParams.put("photoBName", mCurrFileName);
            mVerifyPhotoB.setImageBitmap(smallBitmap);
        }

    }


    public void onSubmitVerifyResponseSuccess() {
        ToastUtil.showShort(App.INSTANCE.getString(R.string.strVerifyIdentActivity221));
        SPUtils.put(MyAppLication.getApplication(), Constants.SP_REALNAME_STATUS, 2);
        setResult(RESULT_OK, new Intent().putExtra("resultData", 2));
        finish();
    }
}
